From: Jonathan Dieter Date: Wed, 21 Mar 2018 18:57:50 +0000 (+0200) Subject: Fix zckdl to work with compressed integers X-Git-Tag: archive/raspbian/1.1.9+ds1-1+rpi1~1^2~353 X-Git-Url: https://dgit.raspbian.org/%22http:/www.example.com/cgi/%22https:/www.github.com/%22bookmarks:///%22http:/www.example.com/cgi/%22https:/www.github.com/%22bookmarks:/?a=commitdiff_plain;h=f2d196f8820255ac44c755e890cf3f678158aa36;p=zchunk.git Fix zckdl to work with compressed integers Signed-off-by: Jonathan Dieter --- diff --git a/src/lib/dl/dl.c b/src/lib/dl/dl.c index 9626250..50397a7 100644 --- a/src/lib/dl/dl.c +++ b/src/lib/dl/dl.c @@ -60,7 +60,7 @@ void zck_dl_free_dl_regex(zckDL *dl) { /* Write zeros to tgt->fd in location of tgt_idx */ int zck_dl_write_zero(zckCtx *tgt, zckIndex *tgt_idx) { char buf[BUF_SIZE] = {0}; - size_t tgt_data_offset = tgt->preindex_size + tgt->index_size; + size_t tgt_data_offset = tgt->header_size + tgt->index_size; size_t to_read = tgt_idx->length; if(!zck_seek(tgt->fd, tgt_data_offset + tgt_idx->start, SEEK_SET)) return False; @@ -153,7 +153,7 @@ int zck_dl_write_range(zckDL *dl, const char *at, size_t length) { &(dl->zck->chunk_hash_type))) return 0; dl->write_in_chunk = idx->length; - size_t offset = dl->zck->preindex_size + + size_t offset = dl->zck->header_size + dl->zck->index_size; if(!zck_seek(dl->dst_fd, offset + tgt_idx->start, SEEK_SET)) @@ -216,8 +216,8 @@ int zck_dl_write_and_verify(zckRangeInfo *info, zckCtx *src, zckCtx *tgt, zckIndex *src_idx, zckIndex *tgt_idx) { static char buf[BUF_SIZE] = {0}; - size_t src_data_offset = src->preindex_size + src->index_size; - size_t tgt_data_offset = tgt->preindex_size + tgt->index_size; + size_t src_data_offset = src->header_size + src->index_size; + size_t tgt_data_offset = tgt->header_size + tgt->index_size; size_t to_read = src_idx->length; if(!zck_seek(src->fd, src_data_offset + src_idx->start, SEEK_SET)) return False; @@ -422,8 +422,6 @@ int zck_zero_bytes(zckDL *dl, size_t bytes, size_t start, size_t *buffer_len) { /* Download header */ int zck_dl_get_header(zckCtx *zck, zckDL *dl, char *url) { - size_t buffer_len = 0; - size_t start = 0; if(zck == NULL) { zck_log(ZCK_LOG_ERROR, "zckCtx not initialized\n"); return False; @@ -432,37 +430,51 @@ int zck_dl_get_header(zckCtx *zck, zckDL *dl, char *url) { zck_log(ZCK_LOG_ERROR, "zckDL not initialized\n"); return False; } + size_t buffer_len = 0; + size_t start = 0; zck->fd = dl->dst_fd; + + /* Download first hundred bytes and read magic and hash type */ if(!zck_dl_bytes(dl, url, 100, start, &buffer_len)) return False; if(!zck_read_initial(zck, dl->dst_fd)) return False; - start += 6; - if(!zck_dl_bytes(dl, url, zck->hash_type.digest_size+9, start, - &buffer_len)) + start = zck_tell(dl->dst_fd); + + /* If we haven't downloaded enough for the index hash plus a few others, do + * it now */ + if(!zck_dl_bytes(dl, url, zck->hash_type.digest_size+start+MAX_COMP_SIZE*2, + start, &buffer_len)) return False; + /* Read and store the index hash */ if(!zck_read_index_hash(zck, dl->dst_fd)) return False; start += zck->hash_type.digest_size; - char *digest = zck_get_index_digest(zck); - zck_log(ZCK_LOG_DEBUG, "Index hash: (%s)", zck_hash_name_from_type(zck_get_full_hash_type(zck))); + zck_log(ZCK_LOG_DEBUG, "Index hash: (%s)", + zck_hash_name_from_type(zck_get_full_hash_type(zck))); for(int i=0; idst_fd)) return False; - start += 2; + start = zck_tell(dl->dst_fd); zck_log(ZCK_LOG_DEBUG, "Index size: %llu\n", zck->index_size); + + /* Download and read rest of index */ if(!zck_dl_bytes(dl, url, zck->index_size, start, &buffer_len)) return False; if(!zck_read_index(zck, dl->dst_fd)) return False; + + /* Write zeros to rest of file */ zckIndexInfo *info = &(dl->info.index); info->hash_type = zck->index.hash_type; zck_log(ZCK_LOG_DEBUG, "Writing zeros to rest of file: %llu\n", zck->index.length + zck->index_size + start); - if(!zck_zero_bytes(dl, zck->index.length, zck->index_size + start, &buffer_len)) + if(!zck_zero_bytes(dl, zck->index.length, zck->header_size + zck->index_size, &buffer_len)) return False; return True; } diff --git a/src/lib/hash/hash.c b/src/lib/hash/hash.c index 673dfc4..384f9fe 100644 --- a/src/lib/hash/hash.c +++ b/src/lib/hash/hash.c @@ -131,7 +131,7 @@ void zck_hash_close(zckHash *hash) { /* Returns 1 if full file hash matches, 0 if it doesn't and -1 if failure */ int zck_hash_check_full_file(zckCtx *zck, int dst_fd) { - if(!zck_seek(dst_fd, zck->preindex_size + zck->index_size, SEEK_SET)) + if(!zck_seek(dst_fd, zck->header_size + zck->index_size, SEEK_SET)) return -1; if(!zck_hash_init(&(zck->check_full_hash), &(zck->hash_type))) return -1; diff --git a/src/lib/io.c b/src/lib/io.c index 900b733..b7dee40 100644 --- a/src/lib/io.c +++ b/src/lib/io.c @@ -110,6 +110,10 @@ int zck_seek(int fd, off_t offset, int whence) { return True; } +size_t zck_tell(int fd) { + return lseek(fd, 0, SEEK_CUR); +} + int zck_chunks_from_temp(zckCtx *zck) { int read_count; char *data = zmalloc(BUF_SIZE); diff --git a/src/lib/zck_private.h b/src/lib/zck_private.h index 2fbec6f..1632ce2 100644 --- a/src/lib/zck_private.h +++ b/src/lib/zck_private.h @@ -78,7 +78,6 @@ typedef struct zckCtx { int temp_fd; int fd; - size_t preindex_size; char *full_hash_digest; char *header_string; size_t header_size; @@ -125,6 +124,7 @@ int zck_write_index(zckCtx *zck); /* io.c */ int zck_seek(int fd, off_t offset, int whence); +size_t zck_tell(int fd); int zck_read(int fd, char *data, size_t length); int zck_write(int fd, const char *data, size_t length); int zck_write_comp_size(int fd, size_t val);